﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL.Repositories
{
    public class UserRepository : RepositoryInterfaces.IUserRepository
    {
        /// <summary>
        /// Get customer by phone number
        /// </summary>
        /// <param name="PhoneNo">Phone number of customer</param>
        /// <returns>Customer with phone number</returns>
        /// <exception cref="ArgumentNullException">Phone number is null</exception>
        /// <exception cref="InvalidOperationException">More than one customer with phone number</exception>
        /// <exception cref="Exception"></exception>
        public User GetCustomer(string phoneNo)
        {
            try
            {
                using (BalderEntities _db = new BalderEntities())
                {
                    return _db.Users.Where(u => u.PhoneNo == phoneNo).Single();
                }
            }
            catch (ArgumentNullException e)
            {
                throw new ArgumentNullException("Phone number is null!" + Environment.NewLine + e.Message);
            }
            catch (InvalidOperationException e)
            {
                throw new InvalidOperationException("More than one customer, or none, with phone number: " + phoneNo + Environment.NewLine + e.Message);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Checks if Employee with initials is authorized
        /// </summary>
        /// <param name="initials">initials of Employee</param>
        /// <returns>true if Employee is authorized</returns>
        /// <exception cref="ArgumentNullException">if initials is null</exception>
        /// <exception cref="Exception"></exception>
        public bool IsEmployeeAuthorized(string initials)
        {
            try
            {
                using (BalderEntities _db = new BalderEntities())
                {
                    return _db.Users.Where(u => u.Initials == initials && u.Type == "Employee").Any();
                }
            }
            catch (ArgumentNullException e)
            {
                throw new ArgumentNullException("Initials is null" + Environment.NewLine + e.Message);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}